import { Location, MapLength } from "../info/Predefine";
import GameManager from "../manager/GameManager";
import PlayerManager from "../manager/PlayerManager";
import Player from "../player/Player";
import UiMiniMap from "./UiMiniMap";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UiPointer extends cc.Component {

    public static instance: UiPointer = null;

    _currLocation: Location = null;

    public get currLocation(): Location {
        return this._currLocation;
    }

    public set currLocation(v: Location) {
        v.locationX = Math.min(Math.max(0, v.locationX), MapLength - 1);
        v.locationY = Math.min(Math.max(0, v.locationY), MapLength - 1);
        let oldLocation = this._currLocation;
        this._currLocation = v;
        this.checkStyleAndPosition(oldLocation, this._currLocation);
        UiMiniMap.instance.updateShotRangeUi(this._currLocation);
    }

    onLoad() {
        this.node.children[0].active = false;
        this.node.children[1].active = false;
        UiPointer.instance = this;
    }

    protected start(): void {
        //监听玩家安家事件
        PlayerManager.instance.controllerPlayer.node.on(Player.EventType.安家, this.onMakeHome, this);
    }

    /**
     * 当玩家安家时
     * @param player 
     */
    onMakeHome(player: Player) {
        //初始位置
        let location = GameManager.instance.getMapLocation(player.node.position);
        this.currLocation = location;
        this.node.children[0].active = true;
        this.node.children[1].active = false;
    }

    /**
     * 根据当前选择的location显示pointer样式
     */
    checkStyleAndPosition(oldLocation: Location, newLocation: Location) {
        let newFac = GameManager.instance.getFacilityByLocation(newLocation);
        let isVisible = PlayerManager.instance.controllerPlayer.isLocationVisible(newLocation);
        if (oldLocation) {
            if (newLocation.locationX == oldLocation.locationX && newLocation.locationY == oldLocation.locationY) return;
            let oldFac = GameManager.instance.getFacilityByLocation(oldLocation);
            let dir = { locationX: newLocation.locationX - oldLocation.locationX, locationY: newLocation.locationY - oldLocation.locationY };
            //跳过占多个地块建筑
            if (oldFac && isVisible && oldFac == newFac) {
                this.currLocation = { locationX: this.currLocation.locationX + dir.locationX, locationY: this.currLocation.locationY + dir.locationY };
                return;
            }
        }
        //设置尺寸        
        if (newFac && isVisible) {
            this.node.setContentSize(newFac.node.getContentSize());
            this.node.position = newFac.node.position;
        } else {
            this.node.setContentSize(80, 80);
            this.node.position = GameManager.instance.getGamePosition(newLocation);
        }

        //判断是否在自己的地盘
        if (PlayerManager.instance.controllerPlayer.isSelfDomain(newLocation)) {
            this.node.children[0].active = true;
            this.node.children[1].active = false;
        } else {
            this.node.children[0].active = false;
            this.node.children[1].active = true;
        }
    }

    refreshSize() {
        let currFac = GameManager.instance.getFacilityByLocation(this.currLocation);
        if (currFac) {
            this.node.setContentSize(currFac.node.getContentSize());
            this.node.position = currFac.node.position;
        }
    }

    // update (dt) {}

    protected onDestroy(): void {
        UiPointer.instance = null;
    }
}
